<template>
	<z-paging ref="paging_car" :refresher-enabled="false" :loading-more-enabled="false" :fixed="false" v-model="form.process_list" @query="queryList">
		<template #top>
			<view class="zpagin_top">
				<nav-bar :title="pageInfo.navbarTitle" :iconLeft="true" />
				<view class="search">
					<view class="text1">生产订单：{{ form.code }}</view>
					<view class="con">
						<view>是否质检：</view>
						<u-radio-group v-model="form.if_check" placement="row">
							<u-radio label="是" :name="1"></u-radio>
							<view style="width: 50rpx;" />
							<u-radio label="否" :name="0"></u-radio>
						</u-radio-group>
					</view>
					<view class="hint-text">本期登记数量应在 1 和 工序数量之间</view>

				</view>
			</view>

		</template>

		<uni-forms :model="form" ref="refForm" label-width="120px">
			<uni-card v-for="(item, pindex) in form.process_list" :key="pindex" margin="10px" spacing="0" :title="item.name">
				<view style="background-color: #f2f5f9;">
					<uni-card v-for="(val, cindex) in item.children" margin="5px" spacing="0" :title="val.name">
						<view class="ctent">名称：{{ val.name }}</view>
						<view class="con">
							<view class="ctent">单次工价：{{ val.price }}</view>
							<view class="ctent">工序数量：{{ val.num }}</view>
						</view>
						<view class="con">
							<view class="ctent">未质检数量：{{ val.no_check_num }}</view>
							<view class="ctent">已完成数：{{ val.up_num }}</view>
						</view>
						<uni-forms-item label="登记用户：" :name="`process_list[${pindex}].children[${cindex}].register_user_id`" required
							:rules="[{ required: true, errorMessage: `请选择【${val.name}】的登记用户` }]">
							<uni-data-select mode="underline" placeholder="请选择登记用户" v-model="val.register_user_id"
								:localdata="staff_list" :clear="false" />
						</uni-forms-item>
						<uni-forms-item label="本期登记数量：">
							<up-number-box v-model="val.now_num" inputWidth="100" :min="1" :max="val.num" :integer="true" />
						</uni-forms-item>
					</uni-card>
				</view>
			</uni-card>
		</uni-forms>
		<template #bottom>
			<view style="padding: 20rpx; background-color: #fff; border-top: 2px solid #e9e9e9;">
				<u-button type="primary" text="提交" @click="save"></u-button>
			</view>
		</template>
	</z-paging>
</template>
<script setup>
import { reactive, ref, getCurrentInstance } from 'vue';
import { onLoad } from '@dcloudio/uni-app'
import pOrderApi from '../../../api/production/p_order'

const pageInfo = reactive({
	navbarTitle: '工序报工',
	edit_id: null,
});

onLoad(option => {
	pageInfo.edit_id = option.id
	pageInfo.navbarTitle = `${pageInfo.navbarTitle}(新增)`
	get_staff_list()
}
)

const refForm = ref(null)
const form = reactive({
	code: '',
	if_check: 1,
	process_list: []
})
const fields = Object.keys(form)

const paging_car = ref(null)
function queryList() {
	pOrderApi.production_order_detail(pageInfo.edit_id).then(data => {
		const record = uni.$lodash.pick(data, fields)
		form.code = record.code
		const list = data.order_products.map(v => {
			return {
				id: v.product.id,
				id_info: 'product_' + v.product.id,
				order_product_id: v.id,
				type: 'product',
				name: v.product.name,
				price: '-',
				num: '-',
				no_check_num: '-',
				up_num: '-',
				register_user_id: null,
				children: v.order_products_process.map(k => ({
					id: k.process.id,
					id_info: 'process_' + k.process.id,
					type: 'process',
					name: k.process.name,
					price: k.process.price,
					num: k.num,
					no_check_num: k.no_check_num,
					up_num: k.up_num,
					now_num: k.num - k.no_check_num - k.up_num,
					register_user_id: null
				}))
			}
		})
		paging_car.value.complete(list);
	})
}

//提交操作
function save() {
	refForm.value.validate().then(() => {
		const params = uni.$lodash.cloneDeep(form) // 深拷贝一份form表单
		pOrderApi.post_register_process(params).then(res => {
			uni.$u.toast('添加成功')
			setTimeout(() => {
				backRefsh();
			}, 1000)
		})
	}).catch((error) => {
		uni.$u.toast(error[0].errorMessage);
	})
}

const staff_list = ref([])
function get_staff_list() {
	pOrderApi.get_staff({ page: 1, page_size: 99999 }).then(res => {
		staff_list.value = res.results.map(v => ({
			text: v.full_name,
			value: v.id
		}))
	})
}
//返回上一级并刷新上一级页面
function backRefsh() {
	uni.navigateBack({
		delta: 1, // 返回的页面层数，1 表示返回上一级
		success: () => {
			uni.$bus.useBusEmit('p_register:refresh')
		}
	})
}
</script>

<style lang="scss" scoped>
.contont {
	height: 100%;
}

.content-bgc {
	height: 100%;

}

.text1 {
	font-weight: 700;
	font-size: 18px;
	color: #0081ff;
}
</style>